<?php

/**
 * @author      :   TrongNV
 * @name        :   My_Global
 * @version     :   1.0
 * @copyright   :   VinhLoi
 * @todo        :   
 */
class My_Global {

    /**
     * Zend_Config_Ini
     * @var Zend_Config_Ini $configuration
     */
    private static $configuration = null;

    /**
     * List mysqli storage
     */
    private static $arrStorage = array();

    /**
     * Storage of master business
     * @var Zend_Db
     */
    private static $arrStorageMasterBusiness = array();

    /**
     * Get application application configuration     
     * @return <object>
     */
    public static function getApplicationIni() {
        //Get Ini Configuration
        if (is_null(self::$configuration)) {
            if (Zend_Registry::isRegistered(APP_CONFIG)) {
                self::$configuration = Zend_Registry::get(APP_CONFIG);
                
            } else {
                self::$configuration = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini');
                Zend_Registry::set(APP_CONFIG, self::$configuration);
            }
        }
        //Return data
        return self::$configuration;
    }

    /**
     * Get master business storage instance
     * @return <Zend_Db>
     */
    public static function getDbMasterBusiness() {
        //Get Ini Configuration
        self::$configuration = self::getApplicationIni();

        //Create object to Connect DB
        self::$arrStorageMasterBusiness = Zend_Db::factory(self::$configuration->resources->db);

        //Changing the Fetch Mode
        self::$arrStorageMasterBusiness->setFetchMode(Zend_Db::FETCH_ASSOC);

        //Create Adapter default is Db_Table
        Zend_Db_Table::setDefaultAdapter(self::$arrStorageMasterBusiness);

        //Push to queue
        self::$arrStorage[] = self::$arrStorageMasterBusiness;

        //Return storage
        return self::$arrStorageMasterBusiness;
    }

    /**
     * Close all mysqli connection
     * @return <bool>
     */
    public static function closeAllDb() {
        foreach (self::$arrStorage as $storage) {
            //Try close
            if (!is_null($storage)) {
                $storage->closeConnection();
            }
        }
        return true;
    }

}

